package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.query.JSONResult;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view(){
        return "department";
    }

    //员工编辑界面需要
    @RequestMapping("list")
    @ResponseBody
    public List<Department> list(){
        return departmentService.selectAll();
    }

    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo){
        return departmentService.query(qo);
    }

    //部门名称检验
    @RequestMapping("validateByName")
    @ResponseBody
    public Boolean validateByName(Long id, String name){
        return departmentService.selectByName(id,name);
    }
    //部门状态改变
    @RequiresPermissions(value={"department:changeStatus","改变部门状态"},logical = Logical.OR)
    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(Long id){
        JSONResult result = new JSONResult();
        try{
            departmentService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //编辑
    @RequiresPermissions(value={"department:edit","部门编辑"},logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Department dept){
        JSONResult result = new JSONResult();
        try{
            dept.setState(true);
            departmentService.saveOrUpdate(dept);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
